﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Locadora.TOB;
namespace Locadora.BLL.DAL
{
    class ComentarioDAL
    {
        internal static List<TB_COMENTARIO> Listar()
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            List<TB_COMENTARIO> lista;
            lista = dc.TB_COMENTARIOs.ToList();
            dc.Dispose();
            return lista;
        }

        internal static List<TB_COMENTARIO> ListarByUsuario(string idUsuario)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var result = (from c in dc.TB_COMENTARIOs where c.CO_USUARIO == idUsuario select c);
            List<TB_COMENTARIO> lista = null;
            foreach (var item in result)
            {
                lista = result.ToList<TB_COMENTARIO>();
                break;
            }
            
            dc.Dispose();
            return lista;
        }

        internal static List<TB_COMENTARIO> ListarByDvd(decimal idDvd)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var result = (from c in dc.TB_COMENTARIOs where c.CO_DVD == idDvd select c);
            return result.ToList<TB_COMENTARIO>();
        }

        internal static void Inserir(TB_COMENTARIO tb)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            dc.TB_COMENTARIOs.InsertOnSubmit(tb);
            dc.SubmitChanges();
            dc.Dispose();
        }

        internal static void Excluir(TB_COMENTARIO tb)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            TB_COMENTARIO result = (from c in dc.TB_COMENTARIOs where c.CO_SEQ_COMENTARIO == tb.CO_SEQ_COMENTARIO select c).First();
            dc.TB_COMENTARIOs.DeleteOnSubmit(result);
            dc.SubmitChanges();
            dc.Dispose();
        }


        internal static int GetTotalComentarios(decimal id)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            return dc.TB_COMENTARIOs.Where(c=>c.CO_DVD == id).Count();
        }

        internal static List<TB_COMENTARIO> GetComentarios(int start, int offset, decimal idDvd)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var users = dc.TB_COMENTARIOs.Where(c=>c.CO_DVD==idDvd).Skip(start).Take(offset);
            return users.ToList();
        }
    }
}
